
$(function () {
    $.idcode.setCode();//加载生成验证码方法

    //显示密码
    $(".eye").on("click", function () {
        if ($('[name="upwd"]').attr("type") == 'password') {
            $('[name="upwd"]').attr("type", "text")
        } else {
            $('[name="upwd"]').attr("type", "password")
        }
    })

    //添加正则规则
    $.validator.addMethod("checkUname", function (val, el, flag) {
        return /^[a-z]\w{5,15}$/i.test(val)
    })
    $.validator.addMethod("checkUphone", function (val, el, flag) {
        return /^[1]\d{12}$/i.test(val)
    })

    //form表单
    $("form").validate({

        //规则
        rules: {
            uname: {
                required: true,//开启必填项
                checkUname: true,
                remote: "http://127.0.0.1:8080/api/checkuser"
            },
            upwd: {
                required: true,
                rangelength: [6, 16]
            },
            uphone: {
                required: true,
            },
            // yzm: {
            //     required: true,
            //     equalTo: "#idcode"//这里不行
            // },
            check: {
                required: true
            }
        },
        //提示消息
        messages: {
            uname: {
                required: "用户名必填",
                checkUname: "<img src='./images/regError.png' />用户名不合法",
                remote: "该用户名已注册"
            },
            upwd: {
                required: "密码必填",
                rangelength: "<img src='./images/regError.png' />密码长度在{0}-{1}"
            },
            uphone: {
                required: "手机必填",
                checkUphone:"<img src='./images/regError.png' />手机号不合法"
            },
            // yzm: {
            //     required: "验证码必填",
            //     equalTo: "<img src='./images/regError.png' />验证码不一致"
            // },
            check: {
                required: "同意协议才可注册"
            }

        },
        //提交
        submitHandler: function () {

            var isBy = $.idcode.validateCode()//验证码一致吗
            if (!isBy) {
                $(".yzmCheck").show()
                return false//阻止默认行为
            } else {
                $(".yzmCheck").hide()
                //把数据发到后台
                $.ajax({
                    url: "http://127.0.0.1:8080/api/reg",
                    type: "post",
                    data: $("form").serialize(),//拿到form表单数据
                    dataType: "json"
                }).then(function (res) {
                    alert(res.msg)
                })
            }

            return false//阻止默认行为
        }
    })
})